package com.ischoolbar.programmer.dao;

/**
 * @Author lin
 **/

import com.ischoolbar.programmer.model.Clazz;
import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClazzDao extends BaseDao {
    public ClazzDao() {
    }

    public List<Clazz> getClazzList(Clazz clazz, Page page) {
        List<Clazz> ret = new ArrayList();
        String sql = "select * from s_clazz ";
        if (!StringUtil.isEmpty(clazz.getName())) {
            sql = sql + "where name like '%" + clazz.getName() + "%'";
        }

        sql = sql + " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = this.query(sql);

        try {
            while(resultSet.next()) {
                Clazz cl = new Clazz();
                cl.setId(resultSet.getInt("id"));
                cl.setName(resultSet.getString("name"));
                cl.setInfo(resultSet.getString("info"));
                ret.add(cl);
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        return ret;
    }

    public int getClazzListTotal(Clazz clazz) {
        int total = 0;
        String sql = "select count(*) as total from s_clazz ";
        if (!StringUtil.isEmpty(clazz.getName())) {
            sql = sql + "where name like '%" + clazz.getName() + "%'";
        }

        ResultSet resultSet = this.query(sql);

        try {
            while(resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }

        return total;
    }

    public boolean addClazz(Clazz clazz) {
        String sql = "insert into s_clazz values(null,'" + clazz.getName() + "','" + clazz.getInfo() + "') ";
        return this.update(sql);
    }

    public boolean deleteClazz(int id) {
        String sql = "delete from s_clazz where id = " + id;
        return this.update(sql);
    }

    public boolean editClazz(Clazz clazz) {
        String sql = "update s_clazz set name = '" + clazz.getName() + "',info = '" + clazz.getInfo() + "' where id = " + clazz.getId();
        return this.update(sql);
    }
}

